/*
 * Copyright 2008. Mount Sinai Hospital, Toronto, Canada.
 * 
 * Licensed under the Apache License, Version 2.0. You
 * can find a copy of the license at:
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * IN NO EVENT SHALL MOUNT SINAI HOSPITAL BE LIABLE TO ANY PARTY FOR DIRECT, 
 * INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST 
 * PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, 
 * EVEN IF MOUNT SINAI HOSPITAL HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH 
 * DAMAGE.
 * 
 * MOUNT SINAI HOSPITAL SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTIES OF 
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE AND 
 * ACCOMPANYING DOCUMENTATION, IF ANY, PROVIDED HEREUNDER IS PROVIDED "AS IS". 
 * MOUNT SINAI HOSPITAL HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, 
 * UPDATES, ENHANCEMENTS, OR MODIFICATIONS. 
 */

package com.sinai.mshab.widgets;

import java.util.HashMap;

import com.google.gwt.user.client.History;
import com.google.gwt.user.client.ui.Widget;

/**
 * The purpose of this TabPanel implementation is to fire off history tokens
 * when a tab is clicked.
 * 
 * @author serge
 * 
 */
public class NotifyingTabPanel extends LazyTabPanel {

	private HashMap<Widget, String> contentToHistory = new HashMap<Widget, String>();

	/**
	 * Adds a widget to the panel with another widget to go in the tab.
	 * @param contentWidget the widget to go to the bottom content container of the panel.
	 * @param tabWidget the widget to go to the tab users click on.
	 * @param history the history token to throw when this tab is selected.
	 */
	public void add(Widget contentWidget, Widget tabWidget, String history) {
		contentToHistory.put(contentWidget, history);
		super.add(contentWidget, tabWidget);
	}

	/**
	 * Adds a widget to the panel with a text label tab.
	 * @param contentWidget the widget to go to the bottom content container of the panel.
	 * @param tabText the text to go into the tab that the users click on.
	 * @param history the history token to throw when this tab is selected.
	 */
	public void add(Widget contentWidget, String tabText, String history) {
		contentToHistory.put(contentWidget, history);
		super.add(contentWidget, tabText);
	}
	
	/**
	 * This method get's called after the tab has been succesfully selected.
	 */
	public void afterTabSelected(int index) {
		Widget widget = this.getWidget(index);
		String token = contentToHistory.get(widget);
		if (token != null) {
			History.newItem(token);
		}
	}
}
